package com.skyz.base.manager;

import android.app.Application;

import com.skyz.base.util.SharedPreferenceUtils;

import java.util.ArrayList;
import java.util.List;

public class EnvironmentManager {
    private static final String ENVIRONMENT_INDEX = "environment";

    private Application mApplication;
    private int mCheckedEnvironmentIndex = 0;

    private final List<Environment> mEnvironmentList = new ArrayList<>();

    private EnvironmentManager() {
    }

    private static class InstanceHolder {
        private static final EnvironmentManager instance = new EnvironmentManager();
    }

    public static EnvironmentManager getInstance() {
        return InstanceHolder.instance;
    }

    public void init(Application application, List<Environment> environmentList) {
        this.mApplication = application;
        if (environmentList != null) {
            mEnvironmentList.addAll(environmentList);
        }
        initCheckedEnvironmentIndex();
    }


    private void initCheckedEnvironmentIndex() {
        if (mApplication == null) {
            return;
        }
        mCheckedEnvironmentIndex = SharedPreferenceUtils.getIntValue(mApplication, ENVIRONMENT_INDEX, 0);
    }

    public void setCheckedEnvironmentIndex(int index) {
        mCheckedEnvironmentIndex = index;
        if (mApplication == null) {
            return;
        }
        SharedPreferenceUtils.setValue(mApplication, ENVIRONMENT_INDEX, mCheckedEnvironmentIndex);
    }

    /**
     * 获取当前所有配置的环境列表
     *
     * @return
     */
    public List<Environment> getEnvironmentList() {
        return mEnvironmentList;
    }

    /**
     * 获取当前选中的环境，删除环境列表可能会导致缓存的 {@link #mCheckedEnvironmentIndex} 超过环境列表大小
     * 此时将重置 {@link #mCheckedEnvironmentIndex}为0后，再返回当前环境
     *
     * @return
     */
    public Environment getCheckedEnvironment() {
        if (mCheckedEnvironmentIndex > mEnvironmentList.size() - 1) {
            setCheckedEnvironmentIndex(0);
        }
        return mEnvironmentList.get(getCheckedEnvironmentIndex());
    }

    public int getCheckedEnvironmentIndex() {
        return mCheckedEnvironmentIndex;
    }

    public static class Environment {
        private String name;
        private String baseUrl;
        private String shareUrl;
        private Object params;

        public Environment(String name, String baseUrl,Object params,String shareUrl) {
            this.name = name;
            this.baseUrl = baseUrl;
            this.params = params;
            this.shareUrl = shareUrl;
        }

        public String getShareUrl() {
            return shareUrl;
        }

        public void setShareUrl(String shareUrl) {
            this.shareUrl = shareUrl;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getBaseUrl() {
            return baseUrl;
        }

        public void setBaseUrl(String baseUrl) {
            this.baseUrl = baseUrl;
        }

        public Object getParams() {
            return params;
        }

        public void setParams(Object params) {
            this.params = params;
        }
    }
}
